<?php
require_once 'models/cms.inc';
require_once 'models/TMSACL.inc';

class TCMSCommentModel extends  TCandyModel {
/**
 * @var TMSACL
 */	
	protected $_ioc_acl_;
	
	protected function afterConstruct(){
		$this->_conf_classname_ = 'TMSComment';
	}

	protected function postProcessProperties(){
		parent::postProcessProperties();
		$this->propertyMetas['AuthorName'] = new TOrmPropertyMeta('AuthorName', 'Author name', TItemPropertyType::PT_STRING);
		$this->propertyMetas['AuthorName']->ReadOnly = true;
		$this->propertyMetas['Author']->ReadOnly = true;
		$this->propertyMetas['CommentText']->ReadOnly = true;
		$this->propertyMetas['Added']->ReadOnly = true;
		$this->propertyMetas['Approved']->ReadOnly = true;
		$this->propertyMetas['ApprovedBy']->ReadOnly = true;
	}
	
	protected function beforeSave(TNOrmObject $object){
		$object = parent::beforeSave($object);
		if (!$object->Added)
			$object->Added = new TDate();
		$u = $this->Acl->CurrentUser();	
		if (is_null($object->Id)){
			$object->IsApproved = false;
			if ($u instanceof TMSUser){
				$object->Author = $u;
				$object->NickName = null;
			}
		}	
		if ($object->IsApproved && is_null($object->ApprovedBy)){
			$object->Approved = new TDate();
			if (($u instanceof TMSUser) && ($u->Roles & TMSRole::MODERATOR))
				$object->ApprovedBy = $u;
			else throw new TCoreException(TCoreException::ERR_ACCESS_DENIED);	
		}
		return $object;
	}
}